﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using MINERP.COM;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Data.Common;

namespace MINERP.DAL.CustomerManage
{
    public class OrderInfoDAL : DALBase
    {
        //no use
        private static ObjectQuery<orderInfo> GetOrderInfoByBagCode(int bagCode)
        {
            ObjectQuery<orderInfo> orderInfos=DataEntites.orderInfo.Where(string.Format("it.Tb_OrderBag.OrderBagId = {0}", bagCode)).Include("Tb_Product");
            DataEntites.Refresh(RefreshMode.StoreWins, orderInfos);
            return orderInfos;
        }
        //no use
        private static ObjectQuery<orderInfo_BK> GetOrderInfoByBagCode_bk(int bagCode)
        {
            ObjectQuery<orderInfo_BK> orderInfos = DataEntites.orderInfo_BK.Where(string.Format("it.Tb_OrderBag_bk.OrderBagId = {0}", bagCode)).Include("Tb_Product");
            DataEntites.Refresh(RefreshMode.StoreWins, orderInfos);
            return orderInfos;
        }

        /// <summary>
        /// 得到订单包下的所有产品
        /// </summary>
        /// <param name="bagCode"></param>
        /// <returns></returns>
        public static List<orderInfo> GetOrderInfoByBag(int bagCode)
        {
            using (newSampleYPDBEntities db=new newSampleYPDBEntities ())
            {
                return db.orderInfo.Include("Tb_Product").Where(o => o.Tb_OrderBag.OrderBagId == bagCode).ToList();
            }
            //List<orderInfo> list = new List<orderInfo>();

            //ObjectQuery<orderInfo> orderInfos = GetOrderInfoByBagCode(bagCode);

            //foreach (orderInfo item in orderInfos)
            //{
            //    list.Add(item);
            //}

            //return list;
        }
        /// <summary>
        /// 得到订单包下的所有产品(备份)
        /// </summary>
        /// <param name="bagCode"></param>
        /// <returns></returns>
        public static List<orderInfo_BK> GetOrderInfoByBag_bk(int bagCode)
        {          
            using (newSampleYPDBEntities db=new newSampleYPDBEntities ())
            {
                return db.orderInfo_BK.Include("Tb_Product").Where(o => o.Tb_OrderBag_BK.OrderBagId == bagCode).ToList();
            }
            //List<orderInfo_BK> list = new List<orderInfo_BK>();

            //ObjectQuery<orderInfo_BK> orderInfos = GetOrderInfoByBagCode_bk(bagCode);

            //foreach (orderInfo_BK item in orderInfos)
            //{
            //    list.Add(item);
            //}

            //return list;
        }

        /// <summary>
        /// 得到订单下的所有产品id
        /// </summary>
        /// <param name="bagID"></param>
        /// <returns></returns>
        internal static List<int> GetOrderInfoIDByBagID(int bagID)
        {
            List<int> list = new List<int>();

            ObjectQuery<int> orderinfos = DataEntites.CreateQuery<int>(string.Format("SELECT VALUE it.OrderProductId FROM orderInfo as it WHERE it.Tb_OrderBag.OrderBagId = {0}", bagID));

            foreach (int orderid in orderinfos)
            {
                list.Add(orderid);
            }

            return list;
        }

        /// <summary>
        /// 取得一个产品的ID从订单中
        /// </summary>
        /// <param name="bagID"></param>
        /// <returns></returns>
        internal static int GetOneOrderInfoIDByBagID(int bagID)
        {

            ObjectQuery<int> orderinfos = DataEntites.CreateQuery<int>(string.Format("SELECT VALUE it.OrderProductId FROM orderInfo as it WHERE it.Tb_OrderBag.OrderBagId = {0}", bagID));

            foreach (int orderid in orderinfos)
            {
                return orderid;
            }

            return 0;
        }
        internal static int GetOneOrderInfoIDByBagID_bk(int bagID)
        {

            ObjectQuery<int> orderinfos = DataEntites.CreateQuery<int>(string.Format("SELECT VALUE it.OrderProductId FROM orderInfo_bk as it WHERE it.Tb_OrderBag_bk.OrderBagId = {0}", bagID));

            foreach (int orderid in orderinfos)
            {
                return orderid;
            }

            return 0;
        }

        /// <summary>
        /// 得到订单下的所有产品ID
        /// </summary>
        /// <param name="bagid"></param>
        /// <returns></returns>
        internal static List<int> GetOrderInfoIDByOrderBagID(int bagid)
        {
            List<int> collection = new List<int>();
            
            collection.AddRange
                (DataEntites.CreateQuery<int>(string.Format("SELECT VALUE it.OrderProductId FROM orderInfo as it WHERE it.Tb_OrderBag.OrderBagId = {0}", bagid)));

            return collection;

        }
        /// <summary>
        /// 得到订单下的所有产品ID(备份)
        /// </summary>
        /// <param name="bagid"></param>
        /// <returns></returns>
        internal static List<int> GetOrderInfoIDByOrderBagID_bk(int bagid)
        {
            List<int> collection = new List<int>();

            collection.AddRange
                (DataEntites.CreateQuery<int>(string.Format("SELECT VALUE it.OrderProductId FROM orderInfo_bk as it WHERE it.Tb_OrderBag_bk.OrderBagId = {0}", bagid)));

            return collection;

        }

        /// <summary>
        /// 从所有的产品中取一张照片
        /// </summary>
        /// <param name="infoid"></param>
        /// <returns></returns>
        public static List<string> GetAllOrderImagePath(int infoid)
        {
            return PhotoInfoDAL.GetAllPathByOrderInfoID(infoid);
        }

        /// <summary>
        /// 根据客户名称模糊匹配
        /// </summary>
        /// <param name="ctmName"></param>
        /// <returns></returns>
        internal static List<int> GetOrderBagIDByCtmName(string ctmName)
        {
            List<int> colletion = new List<int>();
            string patternStr = "%" + ctmName + "%";
            ObjectQuery<int> bagIDs = DataEntites.CreateQuery<int>(string.Format("SELECT VALUE it.Tb_OrderBag.OrderBagId FROM orderInfo as it WHERE it.CtmName like {0}", "'" + patternStr + "'"));
            ObjectQuery<int> bagIDss = bagIDs.Distinct();

            foreach (int bagid in bagIDss)
            {
                colletion.Add(bagid);
            }

            return colletion;
        }
        /// <summary>
        /// 根据客户名称模糊匹配(备份)
        /// </summary>
        /// <param name="ctmName"></param>
        /// <returns></returns>
        internal static List<int> GetOrderBagIDByCtmName_bk(string ctmName)
        {
            List<int> colletion = new List<int>();
            string patternStr = "%" + ctmName + "%";
            ObjectQuery<int> bagIDs = DataEntites.CreateQuery<int>(string.Format("SELECT VALUE it.Tb_OrderBag_bk.OrderBagId FROM orderInfo_bk as it WHERE it.CtmName like {0}", "'" + patternStr + "'"));
            ObjectQuery<int> bagIDss = bagIDs.Distinct();

            foreach (int bagid in bagIDss)
            {
                colletion.Add(bagid);
            }

            return colletion;
        }

        /// <summary>
        /// 判断当前的订单中是否有该流程
        /// </summary>
        /// <param name="flowStatus"></param>
        /// <param name="orderBagid"></param>
        /// <returns></returns>
        public static bool IsHaveFlowStatus(int flowStatus, int orderBagid)
        {
            List<int> productListID = GetOrderInfoIDByOrderBagID(orderBagid);

            foreach (int orderprodcutId in productListID)
            {
                if (WorkerOrderFlowListDAL.GetFlowStatusNameByOrderProduct(orderprodcutId, flowStatus))
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 判断选择的用户在此订单中有没有正在生产的产品
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="orderBagid"></param>
        /// <returns></returns>
        public static bool IsUserProductingOrderBag(int userid, int orderBagid)
        {
            List<int> productListID = GetOrderInfoIDByOrderBagID(orderBagid);

            foreach (int orderprodcutId in productListID)
            {
                if (WorkerOrderFlowListDAL.IsUserProduct(userid, orderprodcutId))
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 得到订单的产品数量
        /// </summary>
        /// <param name="bagid"></param>
        /// <returns></returns>
        public static int GetProductCountByBagID(int bagid)
        {
            ObjectQuery<int> counts = DataEntites.CreateQuery<int>(string.Format("SELECT value count(it.OrderProductId) FROM orderInfo as it WHERE it.Tb_OrderBag.OrderBagId = {0}", bagid));

           foreach (int count in counts)
           {
               return count;
           }

           return 0;
        }

        /// <summary>
        /// 得到停止生产的订单的产品的数量
        /// </summary>
        /// <param name="bagid"></param>
        /// <returns></returns>
        public static int GetProductCountStop(int bagid)
        {
            int status = (int)OrderInfoStatus.Stop;
            ObjectQuery<int> counts = DataEntites.CreateQuery<int>(string.Format("SELECT value count(it.OrderProductId) FROM orderInfo as it WHERE it.Tb_OrderBag.OrderBagId = {0} AND it.status = {1}", bagid, status));

            foreach (int count in counts)
            {
                return count;
            }

            return 0;
        }

        /// <summary>
        /// 获取产品的当前流程ID
        /// </summary>
        /// <param name="orderInfoId"></param>
        /// <returns></returns>
        public static int GetCurrentTechInOrderInfo(int orderInfoId)
        {
            int status = (int)COM.OrderFlowListStatus.Useable;
            int status2 = (int)COM.OrderFlowListStatus.UseableAccept;
            ObjectQuery<WorkOrderFlowList> query = DataEntites.WorkOrderFlowList.Include("tb_Tech").Where(p => p.orderInfo.OrderProductId == orderInfoId && (p.Status == status || p.Status == status2)) as ObjectQuery<WorkOrderFlowList>;
            if (query != null && query.Count() > 0)
            {
                return query.First().tb_Tech.TechId;
            }
            return -1;
        }

        public System.Data.DataTable GetReBackOrderReCord(int orderProductId)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("Proc_GetRebarckRecordByOrderProductId");
            db.AddInParameter(dbCommand, "orderProductId", DbType.Int32, orderProductId);

            return db.ExecuteDataSet(dbCommand).Tables[0];

        }
    }
}
